package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ContactObject;

public class ContactDataGenerator {

	static Random rnd = new Random();

	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts() {
		List<ContactObject> contacts = new ContactDataGenerator()
				.generateRandonContactList(10);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[] { contact });
		}
		return list.iterator();
	}

	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		String line = reader.readLine();
		while (line != null) {
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
					.setFirstname(parts[0])
					.setLastname(parts[1])
					.setAddress(parts[2])
					.setHome(parts[3])
					.setMobile(parts[4])
					.setWork(parts[5])
					.setEmail(parts[6])
					.setEmail2(parts[7])
					.setbDay(parts[8])
					.setbMonth(parts[9])
					.setbYear(parts[10])
					.setAddress2(parts[11])
					.setPhone2(parts[12]);
			line = reader.readLine();
			list.add(new Object[] { contact });
		}
		reader.close();
		return list.iterator();
	}

	public static void main(String[] args) throws IOException {
		if (args.length < 2) {
			System.out.println("Specify 2 parameters: filename and count");
			return;
		}
		String fileName = args[0];
		int count = Integer.parseInt(args[1]);
		new ContactDataGenerator().generateDataToFile(fileName, count);
	}

	private void generateDataToFile(String fileName, int count)
			throws IOException {
		List<ContactObject> contacts = generateRandonContactList(count);
		File file = new File(fileName);
		if (file.exists()) {
			file.delete();
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstname 
					+ "\t" + contact.lastname 
					+ "\t" + contact.address 
					+ "\t" + contact.home 
					+ "\t" + contact.mobile
					+ "\t" + contact.work
					+ "\t" + contact.email
					+ "\t" + contact.email2
					+ "\t" + contact.bDay
					+ "\t" + contact.bMonth
					+ "\t" + contact.bYear
					+ "\t" + contact.address2
					+ "\t" + contact.phone2
					+ "\n");
		}
		writer.close();
	}

	private List<ContactObject> generateRandonContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {
			ContactObject contact = new ContactObject()
					.setFirstname(
							rnd.nextInt() + "firstname")
					.setLastname(
							rnd.nextInt() + "lastname")
					.setAddress(
							"address" + rnd.nextInt())
					.setHome("home" + rnd.nextInt())
					.setMobile("mobile" + rnd.nextInt())
					.setWork("work" + rnd.nextInt())
					.setEmail("email" + rnd.nextInt())
					.setEmail2("email2" + rnd.nextInt())
					.setbDay("1")
					.setbMonth("May")
					.setbYear("1111")
					.setAddress2(
							"address2" + rnd.nextInt())
					.setPhone2("phone2" + rnd.nextInt());
			list.add(contact);
		}
		return list;
	}
}
